package com.jd.dlc.config;

import com.jd.dlc.autoconfigure.DlcProperties;
import com.jd.dlc.lock.DistributedLockManager;
import com.jd.gms.lock.config.JimDbClusterProxy;
import com.jd.gms.lock.utils.JimDbLockUtils;
import com.jd.jim.cli.Cluster;
import com.jd.jim.cli.ReloadableJimClientFactory;
import io.netty.util.HashedWheelTimer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

import static com.jd.dlc.constant.DlcConstant.JIMDB_CLIENT_DEFAULT_NAME;

/**
 * @program: dlc
 * @ClassName JimDBLockConfig
 * @description: Distributed locks can only be used when jimdb is enabled
 * @author: sizegang
 * @create: 2022-05-02
 **/
@ConditionalOnProperty(name = "dlc.jimdb.enabled", havingValue = "true")
@Configuration
public class JimDBLockConfig {

    @Resource
    private DlcProperties dlcProperties;


    @Bean
    public DistributedLockManager distributedLockManager() {
        return new DistributedLockManager();
    }

    /**
     * 如果接入方 已经集成JIMDB 并且 jimdb客户端必须是 jimClient 则使用接入方式的， 此bean不会执行
     *
     * @return
     */
    @Bean(name = JIMDB_CLIENT_DEFAULT_NAME)
    public Cluster jimClient() {
        ReloadableJimClientFactory reloadableJimClientFactory = new ReloadableJimClientFactory();
        reloadableJimClientFactory.setJimUrl(dlcProperties.getJimdb().getJimUrl());
        reloadableJimClientFactory.setConfigId(dlcProperties.getJimdb().getConfigId());
        return reloadableJimClientFactory.getClient();
    }

    /**
     * 主要完成在实现分布式锁时，需要的缓存操作的代理类初始化
     */
    @Bean("jimCluster4LockProxy")
    public JimDbClusterProxy buildClusterProxy(@Autowired @Qualifier("jimClient") Cluster cluster) {
        JimDbClusterProxy proxy = new JimDbClusterProxy();
        proxy.setCluster(cluster);
        return proxy;
    }

    /**
     * 完成对于锁续期的定时轮训器配置
     */
    @Bean("lockTimer")
    public HashedWheelTimer builderTimer() {
        return new HashedWheelTimer();
    }

    /**
     * 完成对分布式锁工具类的初始化
     */
    @Bean("jimDbLockUtils")
    public JimDbLockUtils buildJimDbLockUtil(@Autowired @Qualifier("jimCluster4LockProxy") JimDbClusterProxy proxy,
                                             @Autowired @Qualifier("lockTimer") HashedWheelTimer timer) {
        JimDbLockUtils lockUtils = new JimDbLockUtils();
        lockUtils.setCacheClient4LockProxy(proxy);
        lockUtils.setTimer(timer);
        if (dlcProperties.getJimdb().getUmpAlarmKey() != null) {
            lockUtils.setJimDbLockUtilsUmpKey(dlcProperties.getJimdb().getUmpAlarmKey());
        }
        lockUtils.setReleaseKeyScriptSha(dlcProperties.getJimdb().getReleaseScriptSha());
        lockUtils.setRenewExpirationScriptSha(dlcProperties.getJimdb().getRenewExpirationScriptSha());
        return lockUtils;
    }
}